home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / SecureClassLoader.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.0 KB  |  163 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)SecureClassLoader.java    1.69 98/09/24
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.util.Hashtable;
  18. import java.util.ArrayList;
  19. import java.net.URL;
  20.  
  21. import sun.security.util.Debug;
  22.  
  23. /** 
  24.  * This class extends ClassLoader with additional support for defining
  25.  * classes with an associated code source and permissions which are
  26.  * retrieved by the system policy by default.
  27.  *
  28.  * @version 1.69, 09/24/98
  29.  * @author  Li Gong 
  30.  * @author  Roland Schemers
  31.  */
  32. public class SecureClassLoader extends ClassLoader {
  33.     // Hashtable that maps CodeSource to ProtectionDomain
  34.     private Hashtable pdcache = new Hashtable(11);
  35.  
  36.     private static final Debug debug = Debug.getInstance("scl");
  37.  
  38.     /**
  39.      * Creates a new SecureClassLoader using the specified parent
  40.      * class loader for delegation.
  41.      *
  42.      * <p>If there is a security manager, this method first
  43.      * calls the security manager's <code>checkCreateClassLoader</code> 
  44.      * method  to ensure creation of a class loader is allowed.
  45.      *
  46.      * @param parent the parent ClassLoader
  47.      * @exception  SecurityException  if a security manager exists and its  
  48.      *             <code>checkCreateClassLoader</code> method doesn't allow 
  49.      *             creation of a class loader.
  50.      * @see SecurityManager#checkCreateClassLoader
  51.      */
  52.     protected SecureClassLoader(ClassLoader parent) {
  53.     super(parent);
  54.     // this is to make the stack depth consistent with 1.1
  55.     SecurityManager security = System.getSecurityManager();
  56.     if (security != null) {
  57.         security.checkCreateClassLoader();
  58.     }
  59.     }
  60.  
  61.     /**
  62.      * Creates a new SecureClassLoader using the default parent class
  63.      * loader for delegation.
  64.      *
  65.      * <p>If there is a security manager, this method first
  66.      * calls the security manager's <code>checkCreateClassLoader</code> 
  67.      * method  to ensure creation of a class loader is allowed.
  68.      *
  69.      * @exception  SecurityException  if a security manager exists and its  
  70.      *             <code>checkCreateClassLoader</code> method doesn't allow 
  71.      *             creation of a class loader.
  72.      * @see SecurityManager#checkCreateClassLoader
  73.      */
  74.     protected SecureClassLoader() {
  75.     super();
  76.     // this is to make the stack depth consistent with 1.1
  77.     SecurityManager security = System.getSecurityManager();
  78.     if (security != null) {
  79.         security.checkCreateClassLoader();
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * Converts an array of bytes into an instance of class Class,
  85.      * with an optional CodeSource. Before the
  86.      * class can be used it must be resolved.
  87.      * @param name the name of the class
  88.      * @param b the class bytes
  89.      * @param off the start offset of the class bytes
  90.      * @param len the length of the class bytes
  91.      * @param cs the associated CodeSource, or null if none
  92.      * @return the <code>Class</code> object created from the data,
  93.      *         and optional CodeSource.
  94.      */
  95.     protected final Class defineClass(String name, byte[] b, int off, int len,
  96.                       CodeSource cs)
  97.     {
  98.     if (cs == null)
  99.         return defineClass(name, b, off, len);
  100.     else 
  101.         return defineClass(name, b, off, len, getProtectionDomain(cs));
  102.     }
  103.  
  104.     /**
  105.      * Returns the permissions for the given codesource object.
  106.      * The default implementation of this method invokes the
  107.      * java.security.Policy.getPermissions method to get the permissions
  108.      * granted by the policy to the specified codesource.
  109.      * <p>
  110.      * This method is invoked by the defineClass method that takes
  111.      * a CodeSource as an argument when it is constructing the
  112.      * ProtectionDomain for the class being defined.
  113.      * <p>
  114.      * @param codesource the codesource.
  115.      *
  116.      * @return the permissions granted to the codesource.
  117.      *
  118.      */
  119.     protected PermissionCollection getPermissions(CodeSource codesource)
  120.     {
  121.     Policy p = Policy.getPolicyNoCheck();
  122.  
  123.     PermissionCollection perms;
  124.     if (p == null) {
  125.         return null;
  126.     } else {
  127.         perms = p.getPermissions(codesource);
  128.     }
  129.     return perms;
  130.     }
  131.  
  132.     /*
  133.      * Returned cached ProtectionDomain for the specified CodeSource.
  134.      */
  135.     private ProtectionDomain getProtectionDomain(CodeSource cs) {
  136.     if (cs == null)
  137.         return null;
  138.  
  139.     ProtectionDomain pd = (ProtectionDomain)pdcache.get(cs);
  140.     if (pd == null) {
  141.         synchronized (pdcache) {
  142.         pd = (ProtectionDomain)pdcache.get(cs);
  143.         if (pd == null) {
  144.  
  145.             PermissionCollection perms = getPermissions(cs);
  146.             if (debug != null) {
  147.             debug.println(" getPermissions "+ cs);
  148.             debug.println("  "+perms);
  149.             debug.println("");
  150.             }
  151.             pd = new ProtectionDomain(cs, perms);
  152.  
  153.             if (pd != null) {
  154.             pdcache.put(cs, pd);
  155.             }
  156.         }
  157.         }
  158.     }
  159.     return pd;
  160.     }
  161.  
  162. }
  163.